package com.grasp.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.grasp.base.Logger;

public abstract class BaseFragment extends Fragment {
	protected View mView;
	private boolean init = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Logger.d(this.getClass().getName() + "-->onCreate()");
	}

	@Override
	public final View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		Logger.d(this.getClass().getName() + "-->onCreateView()");
		if (mView == null) {
			initView(inflater, container, savedInstanceState);
		} else {
			ViewGroup mGroup = (ViewGroup) mView.getParent();
			if (mGroup != null) {
				mGroup.removeView(mView);
			}
			refreshView();
		}
		return mView;
	}

	/**
	 * 返回该Fragment
	 */
	protected void refreshView() {

	}

	/**
	 * 初始化进入的业务逻辑
	 */
	protected abstract void main();

	/**
	 * 初始化 布局
	 * 
	 * @param inflater
	 * @param container
	 * @param savedInstanceState
	 */
	protected abstract void initView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState);

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Logger.d(this.getClass().getName() + "-->onActivityCreated()");
	}

	@Override
	public void onStart() {
		super.onStart();
		Logger.d(this.getClass().getName() + "-->onStart()");
		if (init) {
			main();
			init = false;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Logger.d(this.getClass().getName() + "-->onResume()");
	}

	@Override
	public void onPause() {
		super.onPause();
		Logger.d(this.getClass().getName() + "-->onPause()");
	}

	@Override
	public void onStop() {
		super.onStop();
		Logger.d(this.getClass().getName() + "-->onStop()");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Logger.d(this.getClass().getName() + "-->onDestroyView()");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Logger.d(this.getClass().getName() + "-->onDestroy()");
	}

	/**
	 * 
	 * @param fragment
	 *            跳转的目标
	 * @param addBack
	 *            是否压栈
	 */
	protected final void nextFragment(Fragment fragment, boolean addBack) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(R.id.realtabcontent, fragment);
		if (addBack) {
			ft.addToBackStack(null);
		}
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.commit();
	}

	/**
	 * 
	 * @param fragment
	 *            跳转的目标
	 * @param addBack
	 *            是否压栈
	 */
	protected final void nextFragment(Fragment fragment, boolean addBack, int id) {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(id, fragment);
		if (addBack) {
			ft.addToBackStack(null);
		}
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.commit();
	}

	/**
	 * 
	 * @param fragment
	 *            跳转的目标
	 * @param addBack
	 *            是否压栈
	 * @param visible
	 *            是否隐藏主界面TAB
	 */
	protected final void nextFragment(Fragment fragment, boolean addBack,
			boolean visible) {
		nextFragment(fragment, addBack);
	}

	@Override
	public void onDetach() {
		super.onDetach();
	}
}
